package com.app.conrtoller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSON;
import com.app.domain.ExternalResponse;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.ihyfb.IhyfbResponseResult;
import com.app.domain.ihyfb.LHmudjack;
import com.app.domain.ihyfb.TokenResponseResult;
import com.app.service.UploadParameterService;
import com.app.service.UploadService;
import com.app.service.YjDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/ihyfb")
public class IhyfbUploadController {
    @Value("${yj.ihyfb.serviceHost}")
    private String serviceHost;
    @Value("${yj.ihyfb.refreshToken}")
    private String refreshTokenUrl;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    UploadService uploadService;


    @RequestMapping("/upload")
    @ResponseBody
    public ExternalResponse upload(@RequestParam(value = "ids[]") String[] ids) throws InterruptedException {
        ExternalResponse externalResponse = null;
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("appId", "zlyjd3b1");
        tokenMap.put("sk", "a5b83587e400184a21df8ccfcb13212c");
        String token;
        TokenResponseResult tokenResponseResult = uploadService.sendIhyfbToken(refreshTokenUrl, tokenMap);
        if (tokenResponseResult == null || !tokenResponseResult.getStatus().equals("0")) {
            externalResponse = new ExternalResponse(0, "刷新token已过期,请联系系统管理员", "");
            return externalResponse;
        }
        token = tokenResponseResult.getToken();
        int total = ids.length;
        int succTotal = 0;
        int failTotal = 0;
        StringBuffer responseContent = new StringBuffer();
        responseContent.append("本次共上传数据：【" + total + "】条,");

        for (String id : ids) {
            YjData yjData = yjDataService.findOne(id);
            UploadParameter uploadParameter = uploadParameterService.findOne("2");
            List<LHmudjack> mudjackList = new ArrayList<>();
            mudjackList.add(uploadService.getLHmudjack(token, yjData, uploadParameter));
            String mudjackListJson = JSON.toJSONString(mudjackList);
            IhyfbResponseResult ihyfbResponseResult = uploadService.sendIhyfb(serviceHost, mudjackListJson);
            if (ihyfbResponseResult==null||!ihyfbResponseResult.getStatus().equals("200")){
                log.info("-------------压浆记录数据发送失败------------------------");
                updateState("0", String.valueOf(yjData.getId()));
                failTotal++;
            }else {
                updateState("1", String.valueOf(yjData.getId()));
                succTotal++;
            }
        }   
        responseContent.append("成功【" + succTotal + "】条,失败" + failTotal + "条");
        externalResponse = new ExternalResponse(0, responseContent.toString(), "");
        return externalResponse;
    }





    private void updateState(String state, String id) {
        yjDataService.updateById(state, id);
    }
}
